package com.jozen.smart_toilet_system.ui.common_toile_room_tag_view;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.jozen.modelbase.netwoek.bean.HomeModel;
import com.jozen.smart_toilet_system.BaseApplication;
import com.jozen.smart_toilet_system.R;

import java.util.List;

public class RecyclerViewAdapter2 extends RecyclerView.Adapter<RecyclerViewAdapter2.ViewHolder> {
    private List<HomeModel.ListKeliu> data;

    public RecyclerViewAdapter2(List<HomeModel.ListKeliu> data) {
        this.data = data;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_keliu, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        HomeModel.ListKeliu item = data.get(getActualPosition(position));
        // 设置数据到ViewHolder中的视图
        holder.bind(item);
    }

    @Override
    public int getItemCount() {
        return data.size();
    }
    private int getActualPosition(int position) {
        if (data.size() > 3) {
            int totalCount = 2 * data.size() - 1; // 总共的项数，包括首尾相连的部分
            int realPosition = position % totalCount; // 得到实际位置

            if (realPosition < data.size()) {
                return realPosition;
            } else {
                return totalCount - realPosition;
            }
        } else {
            return position;
        }
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private TextView tv1, tv2, tv3, tv4, tv5;
        private LinearLayout rootView;
        private ImageView image;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            rootView = itemView.findViewById(R.id.rootView);
            tv1 = itemView.findViewById(R.id.tv1);
            tv2 = itemView.findViewById(R.id.tv2);
            tv3 = itemView.findViewById(R.id.tv3);
            tv4 = itemView.findViewById(R.id.tv4);
            tv5 = itemView.findViewById(R.id.tv5);
            image = itemView.findViewById(R.id.image);
        }

        public void bind(HomeModel.ListKeliu item) {
            switch (item.getType()) { //1 总日 2 男厕 3 女厕 4 第三卫
                case 1:
                    tv1.setText("总客流");
                    tv1.setTextColor(BaseApplication.getInstance().getResources().getColor(R.color.color_649F78));
                    tv3.setTextColor(BaseApplication.getInstance().getColor(R.color.color_649F78));
                    tv5.setTextColor(BaseApplication.getInstance().getColor(R.color.color_649F78));
                    image.setImageResource(R.drawable.ic_total);
                    rootView.setBackgroundResource(R.drawable.shape_keliu_total);
                    break;
                case 2:
                    image.setImageResource(R.drawable.ic_man);
                    tv1.setTextColor(BaseApplication.getInstance().getColor(R.color.color_5D83AA));
                    tv3.setTextColor(BaseApplication.getInstance().getColor(R.color.color_5D83AA));
                    tv5.setTextColor(BaseApplication.getInstance().getColor(R.color.color_5D83AA));
                    tv1.setText("男厕");
                    rootView.setBackgroundResource(R.drawable.shape_keliu_man);
                    break;
                case 3:
                    image.setImageResource(R.drawable.ic_female);
                    tv1.setTextColor(BaseApplication.getInstance().getColor(R.color.color_CF7C7C));
                    tv3.setTextColor(BaseApplication.getInstance().getColor(R.color.color_CF7C7C));
                    tv5.setTextColor(BaseApplication.getInstance().getColor(R.color.color_CF7C7C));
                    tv1.setText("女厕");
                    rootView.setBackgroundResource(R.drawable.shape_keliu_female);
                    break;
                case 4:
                    image.setImageResource(R.drawable.ic_third);
                    tv1.setTextColor(BaseApplication.getInstance().getColor(R.color.color_A58959));
                    tv3.setTextColor(BaseApplication.getInstance().getColor(R.color.color_A58959));
                    tv5.setTextColor(BaseApplication.getInstance().getColor(R.color.color_A58959));
                    tv1.setText("第三卫");
                    rootView.setBackgroundResource(R.drawable.shape_keliu_third);
                    break;
            }
            tv2.setText(item.getNu() + "");
            tv4.setText(item.getTotalNu() + "");
        }
    }
}